package org.jeecg.modules.openai.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.openai.entity.AiOrder;
import org.jeecg.modules.openai.mapper.AiOrderMapper;
import org.jeecg.modules.openai.service.IAiOrderService;
import org.jeecg.modules.openai.util.WeChatPayment;
import org.jeecg.modules.openai.wx.WechatService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 系统订单表
 * @Author: jeecg-boot
 * @Date: 2023-03-01
 * @Version: V1.0
 */
@Service
@Slf4j
public class AiOrderServiceImpl extends ServiceImpl<AiOrderMapper, AiOrder> implements IAiOrderService {

    @Autowired
    private ISysUserService userService;

    @Override
    public void paySuccess(String paymentId) {

        if (update(new UpdateWrapper<AiOrder>().set("payment_state", CommonConstant.PAY_SUCCESS).eq("payment_id", paymentId))) {
            AiOrder order = query().eq("payment_id", paymentId).one();
            //支付成功执行积分订单逻辑
            if (CommonConstant.AI_ORDER_SCORE.equals(order.getType())) {
                userService.updateUserScore(order.getUserId(), order.getScore());
            }
        } else {
            log.info("已经处理过这笔订单了:{}", paymentId);
        }
    }

    private final Integer AD_ORDER = 1;
    private final Integer SHARE_ORDER = 2;
    private final static Integer SCORE_ORDER_6 = 3; //6元积分订单

    private final static Integer SCORE_ORDER_10 = 4; //6元积分订单
    private final Integer SCORE_ORDER_50 = 5; //50元积分订单
    private final Integer SCORE_ORDER_100 = 6; //100元积分订单
    private final Integer price = 1000; // 1元人民币可以兑换多少积分

    @Override
    public JSONObject createScoreOrder(Integer type, String paymentId) {

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        AiOrder order = new AiOrder();
        SysUser user = userService.getById(sysUser.getId());
        order.setUserId(sysUser.getId());

        if (SCORE_ORDER_6 == type) {
            order.setScore(6 * price);
            order.setTotal(6*100);
        }

        if (SCORE_ORDER_10 == type) {
            order.setScore(12 * price);
            order.setTotal(10*100);
        }

        if (SCORE_ORDER_50 == type) {
            order.setScore(60 * price);
            order.setTotal(50*100);
        }

        if (SCORE_ORDER_100 == type) {
            order.setScore(135 * price);
            order.setTotal(100*100);
        }

        order.setType(CommonConstant.AI_ORDER_SCORE); //设置积分订单
        save(order);
        order.setPaymentId(order.getId());
        updateById(order);

        return WeChatPayment.createPayment(order, user.getOpenId());

    }


}
